package com.hopes.bizz.stop.risk.alarm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.hopes.bizz.common.core.util.DateUtils;
import com.hopes.bizz.stop.risk.alarm.dto.LineAnalyseAlarmDTO;
import com.hopes.bizz.stop.risk.alarm.service.LineAlarmBrokenService;
import com.hopes.bizz.stop.risk.alarm.service.LineAlarmExceptionService;
import com.hopes.bizz.stop.risk.alarm.service.LineAlarmOverproofService;
import com.hopes.bizz.stop.risk.alarm.service.LineAlarmStatisticsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: ljh
 * @create: 2024/2/23
 */
@Service
@RequiredArgsConstructor
public class LineAlarmStatisticsServiceImpl implements LineAlarmStatisticsService {


    private final LineAlarmExceptionService lineAlarmExceptionService;
    private final LineAlarmOverproofService lineAlarmOverproofService;
    private final LineAlarmBrokenService lineAlarmBrokenService;

    @Override
    public JSONArray alarmDataAnalyseByDay(LineAnalyseAlarmDTO analyseAlarmDTO) {
        JSONArray source = new JSONArray();
        Date beginTime = analyseAlarmDTO.getBeginTime();
        Date endTime = analyseAlarmDTO.getEndTime();
        Map<String, Long> exceptionMap = lineAlarmExceptionService.query()
                .ge(ObjectUtil.isNotNull(beginTime), "DATE_FORMAT (alarm_time,'%Y-%m-%d')", DateUtils.formatDate(beginTime, DateUtils.FORMAT_DAY))
                .le(ObjectUtil.isNotNull(endTime), "DATE_FORMAT (alarm_time,'%Y-%m-%d')", DateUtils.formatDate(endTime, DateUtils.FORMAT_DAY))
                .list().stream().collect(Collectors.groupingBy(item -> DateUtils.limitDay(item.getAlarmTime()), Collectors.counting()));
        Map<String, Long> overproofMap = lineAlarmOverproofService.query()
                .ge(ObjectUtil.isNotNull(beginTime), "DATE_FORMAT (alarm_time,'%Y-%m-%d')", DateUtils.formatDate(beginTime, DateUtils.FORMAT_DAY))
                .le(ObjectUtil.isNotNull(endTime), "DATE_FORMAT (alarm_time,'%Y-%m-%d')", DateUtils.formatDate(endTime, DateUtils.FORMAT_DAY)).list()
                .stream().collect(Collectors.groupingBy(item -> DateUtils.limitDay(item.getAlarmTime()), Collectors.counting()));
        Map<String, Long> brokenMap= lineAlarmBrokenService.query()
                .ge(ObjectUtil.isNotNull(beginTime), "DATE_FORMAT (alarm_time,'%Y-%m-%d')", DateUtils.formatDate(beginTime, DateUtils.FORMAT_DAY))
                .le(ObjectUtil.isNotNull(endTime), "DATE_FORMAT (alarm_time,'%Y-%m-%d')", DateUtils.formatDate(endTime, DateUtils.FORMAT_DAY)).list()
                .stream().collect(Collectors.groupingBy(item -> DateUtils.limitDay(item.getAlarmTime()), Collectors.counting()));
        // 折线图
        JSONArray overproof = new JSONArray();
        overproof.add("超标报警");
        JSONArray exception = new JSONArray();
        exception.add("异常报警");
        JSONArray broken = new JSONArray();
        broken.add("断网报警");
        Set<String> timeList = DateUtils.makeBeginTimeToEndTimeList(beginTime, endTime, DateUtils.FORMAT_DAY);
        JSONArray timeArray = new JSONArray();
        timeArray.add("time");
        timeArray.addAll(timeList);
        for (String time : timeList) {
            overproof.add(overproofMap.getOrDefault(time, 0L));
            exception.add(exceptionMap.getOrDefault(time, 0L));
            broken.add(brokenMap.getOrDefault(time, 0L));
        }
        source.add(timeArray);
        source.add(overproof);
        source.add(exception);
        source.add(broken);
        return source;
    }

    @Override
    public JSONArray alarmDataAnalyseByMonth(LineAnalyseAlarmDTO analyseAlarmDTO) {
        JSONArray source = new JSONArray();
        Date beginTime = analyseAlarmDTO.getBeginTime();
        Date endTime = analyseAlarmDTO.getEndTime();
        Map<String, Long> exceptionMap = lineAlarmExceptionService.query()
                .ge(ObjectUtil.isNotNull(beginTime), "DATE_FORMAT (alarm_time,'%Y-%m')", DateUtils.formatDate(beginTime, DateUtils.FORMAT_MOUTH))
                .le(ObjectUtil.isNotNull(endTime), "DATE_FORMAT (alarm_time,'%Y-%m')", DateUtils.formatDate(endTime, DateUtils.FORMAT_MOUTH))
                .list().stream().collect(Collectors.groupingBy(item -> DateUtils.limitMouth(item.getAlarmTime()), Collectors.counting()));
        Map<String, Long> overproofMap = lineAlarmOverproofService.query()
                .ge(ObjectUtil.isNotNull(beginTime), "DATE_FORMAT (alarm_time,'%Y-%m')", DateUtils.formatDate(beginTime, DateUtils.FORMAT_MOUTH))
                .le(ObjectUtil.isNotNull(endTime), "DATE_FORMAT (alarm_time,'%Y-%m')", DateUtils.formatDate(endTime, DateUtils.FORMAT_MOUTH)).list()
                .stream().collect(Collectors.groupingBy(item -> DateUtils.limitMouth(item.getAlarmTime()), Collectors.counting()));
        Map<String, Long> brokenMap= lineAlarmBrokenService.query()
                .ge(ObjectUtil.isNotNull(beginTime), "DATE_FORMAT (alarm_time,'%Y-%m')", DateUtils.formatDate(beginTime, DateUtils.FORMAT_MOUTH))
                .le(ObjectUtil.isNotNull(endTime), "DATE_FORMAT (alarm_time,'%Y-%m')", DateUtils.formatDate(endTime, DateUtils.FORMAT_MOUTH)).list()
                .stream().collect(Collectors.groupingBy(item -> DateUtils.limitMouth(item.getAlarmTime()), Collectors.counting()));
        // 折线图
        JSONArray overproof = new JSONArray();
        overproof.add("超标报警");
        JSONArray exception = new JSONArray();
        exception.add("异常报警");
        JSONArray broken = new JSONArray();
        broken.add("断网报警");
        Set<String> timeList = DateUtils.makeBeginTimeToEndTimeList(beginTime, endTime, DateUtils.FORMAT_MOUTH);
        JSONArray timeArray = new JSONArray();
        timeArray.add("time");
        timeArray.addAll(timeList);
        for (String time : timeList) {
            overproof.add(overproofMap.getOrDefault(time, 0L));
            exception.add(exceptionMap.getOrDefault(time, 0L));
            broken.add(brokenMap.getOrDefault(time, 0L));
        }
        source.add(timeArray);
        source.add(overproof);
        source.add(exception);
        source.add(broken);
        return source;
    }

    @Override
    public JSONArray alarmDataAnalyseByYear(LineAnalyseAlarmDTO analyseAlarmDTO) {
        JSONArray source = new JSONArray();
        Date beginTime = analyseAlarmDTO.getBeginTime();
        Date endTime = analyseAlarmDTO.getEndTime();
        Map<String, Long> exceptionMap = lineAlarmExceptionService.query()
                .ge(ObjectUtil.isNotNull(beginTime), "DATE_FORMAT (alarm_time,'%Y')", DateUtils.formatDate(beginTime, DateUtils.FORMAT_YEAR))
                .le(ObjectUtil.isNotNull(endTime), "DATE_FORMAT (alarm_time,'%Y')", DateUtils.formatDate(endTime, DateUtils.FORMAT_YEAR))
                .list().stream().collect(Collectors.groupingBy(item -> DateUtils.limitYear(item.getAlarmTime()), Collectors.counting()));
        Map<String, Long> overproofMap = lineAlarmOverproofService.query()
                .ge(ObjectUtil.isNotNull(beginTime), "DATE_FORMAT (alarm_time,'%Y')", DateUtils.formatDate(beginTime, DateUtils.FORMAT_YEAR))
                .le(ObjectUtil.isNotNull(endTime), "DATE_FORMAT (alarm_time,'%Y')", DateUtils.formatDate(endTime, DateUtils.FORMAT_YEAR)).list()
                .stream().collect(Collectors.groupingBy(item -> DateUtils.limitYear(item.getAlarmTime()), Collectors.counting()));
        Map<String, Long> brokenMap= lineAlarmBrokenService.query()
                .ge(ObjectUtil.isNotNull(beginTime), "DATE_FORMAT (alarm_time,'%Y')", DateUtils.formatDate(beginTime, DateUtils.FORMAT_YEAR))
                .le(ObjectUtil.isNotNull(endTime), "DATE_FORMAT (alarm_time,'%Y')", DateUtils.formatDate(endTime, DateUtils.FORMAT_YEAR)).list()
                .stream().collect(Collectors.groupingBy(item -> DateUtils.limitYear(item.getAlarmTime()), Collectors.counting()));
        // 折线图
        JSONArray overproof = new JSONArray();
        overproof.add("超标报警");
        JSONArray exception = new JSONArray();
        exception.add("异常报警");
        JSONArray broken = new JSONArray();
        broken.add("断网报警");
        Set<String> timeList = DateUtils.makeBeginTimeToEndTimeList(beginTime, endTime, DateUtils.FORMAT_YEAR);
        JSONArray timeArray = new JSONArray();
        timeArray.add("time");
        timeArray.addAll(timeList);
        for (String time : timeList) {
            overproof.add(overproofMap.getOrDefault(time, 0L));
            exception.add(exceptionMap.getOrDefault(time, 0L));
            broken.add(brokenMap.getOrDefault(time, 0L));
        }
        source.add(timeArray);
        source.add(overproof);
        source.add(exception);
        source.add(broken);
        return source;
    }
}
